﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.Entities;
using WMS.ProductcategoryTable.DTO;

namespace WMS.ProductcategoryTable
{
    public class ProductcategoryAppService : ApplicationService, IProductcategoryAppService
    {
        private readonly IRepository<Productcategory,int> _productcategoryRepository;

        public ProductcategoryAppService(IRepository<Productcategory, int> productcategoryRepository)
        {
            _productcategoryRepository = productcategoryRepository;
        }

        /// <summary>
        /// 商品分类显示
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetProductcateInfo(int page, int limit,string? productname)
        {
            var prduct = PredicateBuilder.New<Productcategory>(true);
            if (!string.IsNullOrEmpty(productname))
            {
                prduct = prduct.And(m => m.ProductNam.Contains(productname));
            }
            var productcatelist = _productcategoryRepository.GetListAsync(prduct).Result;
            PagedResult result = new PagedResult();
            result.CurrentPage = page;
            result.PageSize = limit;
            result.RowCount = productcatelist.Count;
            result.PageCount = (int)Math.Ceiling((decimal)productcatelist.Count / limit);
            result.Queryable = productcatelist.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return result;
        }

        /// <summary>
        /// 商品分类添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> CreateProducate(CreateProductcategoryDTO dto)
        {
            var producate = ObjectMapper.Map<CreateProductcategoryDTO, Productcategory>(dto);
            await _productcategoryRepository.InsertAsync(producate);
            return 1;
        }

        /// <summary>
        /// 商品分类删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> DeleteProducate(int id)
        {
            await _productcategoryRepository.DeleteAsync(id);
            return 1;
        }

        /// <summary>
        /// 根据商品分类的ID获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<GetProductcategoryDTO> FanTianProducategoryInfo(int id)
        {
            var producate = _productcategoryRepository.GetAsync(id).Result;
            var info = ObjectMapper.Map<Productcategory,GetProductcategoryDTO>(producate);
            return info;
        }

        /// <summary>
        /// 商品分类修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdateProducate(UpdateProductcategoryDTO dto)
        {
            var producate = ObjectMapper.Map<UpdateProductcategoryDTO, Productcategory>(dto);
            await _productcategoryRepository.UpdateAsync(producate);
            return 1;
        }
    }
}
